package com.example.server.controller;

import com.example.server.entity.User;
import com.example.server.mapper.UserMapper;
import com.example.server.service.UserService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {
   @Autowired
    UserService userService;
   @Autowired
    UserMapper userMapper;

    @GetMapping("/")
    public List<User> index()
    {return userService.findAll();}
    @GetMapping("/findByName")
    public List<User> findByName(@RequestParam String username) {
        return userService.findByName(username);
    }
    //url传参(参数名与前台参数名必须一致)
    @DeleteMapping ("/{id}")
    public Integer delete(@PathVariable Integer id)
    {
        return userService.delete(id);
    }
    //请求传参(参数名与前台参数名必须一致)
    @GetMapping("/page")
    public Map<String,Object> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize)
    {
        pageNum=(pageNum-1)*pageSize;
        List<User> data=userMapper.selectPage(pageNum,pageSize);
        Integer total=userMapper.selectTotal();
        Map<String,Object> res=new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return res;
    }
    //请求传参表单
    @PostMapping("/add")
    public Integer save(@RequestBody User u)
    {
        System.out.println(u);
        return userService.save(u);
    }
}